Option Explicit

Sub MakeWikiTable()
    Dim sht As Worksheet
    Dim bool As Boolean
    Dim rngSero As Range
    Dim rngS As Range
    Dim rngGaro As Range
    Dim rngG As Range
    Dim colCnt As Long
    
    '결과 시트 생성 ==========================
    For Each sht In Worksheets
        If sht.Name = "결과" Then bool = True
    Next sht
    
    If bool = False Then Call MakeResultSheet
    Call ResultSheetClear
    
    '위키 테이블 생성 =========================
    Set sht = Worksheets("결과")
    Set rngSero = Range("a1", Range("a65535").End(xlUp))
    Let colCnt = Range("iv1").End(xlToLeft).Column
    
    sht.Range("a1") = "{| class=""wikitable"""
    For Each rngS In rngSero
        Set rngGaro = Range(rngS, rngS.Offset(0, colCnt - 1))
        For Each rngG In rngGaro
            If rngG.Row = 1 Then
                If rngG.Column = 1 Then
                    sht.Range("a" & Rows.Count).End(xlUp)(2) = "|-"
                    sht.Range("a" & Rows.Count).End(xlUp)(2) = "! " & Format(rngG, "#,###")
                Else
                    sht.Range("a" & Rows.Count).End(xlUp) = sht.Range("a" & Rows.Count).End(xlUp) & " !! " & Format(rngG, "#,###")
                End If
            Else
                If rngG.Column = 1 Then
                    sht.Range("a" & Rows.Count).End(xlUp)(2) = "|-"
                    sht.Range("a" & Rows.Count).End(xlUp)(2) = "| " & Format(rngG, "#,###")
                Else
                    sht.Range("a" & Rows.Count).End(xlUp) = sht.Range("a" & Rows.Count).End(xlUp) & " || " & Format(rngG, "#,###")
                End If
            End If
        Next rngG
    Next rngS
    sht.Range("a" & Rows.Count).End(xlUp)(2) = "|}"
    
    Worksheets("결과").Activate
End Sub


Sub MakeResultSheet()
    Dim sht As Worksheet
    Sheets.Add
    Set sht = ActiveSheet
    sht.Name = "결과"
    sht.Move after:=Sheets(Sheets.Count)
    Worksheets(1).Activate
End Sub


Sub ResultSheetClear()
    Dim sht As Worksheet
    Set sht = Worksheets("결과")
    sht.Cells.Clear
End Sub
